package com.itany.mall.dao.mapper;

import java.sql.ResultSet;

import com.itany.mall.entity.Product;
import com.itany.mall.entity.ProductType;
import com.itany.mall.jdbc.template.callback.RowMapper;

public class ProductRowMapper implements RowMapper<Product> {

	public String selectSql() {
		StringBuffer sql = new StringBuffer();
		sql.append(" select ");
		sql.append("     p.id pid, p.name pname, p.price, p.num, ");
		sql.append("     t.id tid, t.name tname ");
		sql.append(" from t_product p ");
		sql.append("     left join t_product_type t ");
		sql.append("         on p.type_id = t.id ");
		return sql.toString();
	}

	@Override
	public Product mapRow(ResultSet res) throws Exception {
		Product p = new Product();
		p.setId(res.getLong("pid"));
		p.setName(res.getString("pname"));
		p.setPrice(res.getDouble("price"));
		p.setNum(res.getInt("num"));
		p.setTypeId(res.getLong("tid"));
		ProductType t = new ProductType();
		t.setId(res.getLong("tid"));
		t.setName(res.getString("tname"));
		p.setType(t);
		return p;
	}

}
